<?php

namespace Home\Model;
use Think\Model;
class SmsLogModel extends BaseModel{
    
    //短信log表
    protected $tableName = 'tp_sms_log';
    
    /**
     * @desc 写入短信记录
     * @author quyang
     * @update 2016-12-2
     */
    public function sendSms($data,$msgId,$notice,$content,$num,$sendtype){
        $times = date('YmdHis',time());
        $where = array(
            'f_parcel_id'=>$data['packageCode'],
            'f_massage_id'=>$msgId,
            'f_send_time'=>$times,
            'f_type'=>'DX',
            'f_mail_no'=>$data['mailNo'],
            'f_recipient_mobile'=>$data['mobile'],
            'f_sms_content'=>$content,
            'f_send_notice'=>$notice,    //短信发送状态
            'f_station_id'=>$data['stationId'],
            'f_reduce_num'=>$num,    //短信扣费条数
            'f_send_sms_type'=>$sendtype);   //短信发送类型
        $this->add($where);
    }
    
    /**
     * @desc 查询短信log
     * @author quyang
     * @update 2016-12-2
     */
    public function querySms($msgId,$status,$error){
        $where = array('f_massage_id'=>$msgId);
        $data = $this->where($where)->find();
        if ($data == false){
            return false;
        }

        $map = array(
                        'f_sms_notice'              =>$status,
                        'f_sms_success_time'        =>date('YmdHis'),
                        'f_sms_error'               =>$error,
                        'f_sms_success_time_stamp'  =>time()
                     );

        //根据返回状态更改发送状态
        $Notice = new \Home\Server\ParcelNoticeResultServer();
        $Notice->updateParcelNotice( $data['f_parcel_id'] , $map );

        //更新log表回调状态
        $this->where($where)->save(array('f_success_notice'=>$error,'f_success_time'=>date('YmdHis',time())));
        //根据发送类型判断是否修改包裹数据   12-22
        if($data['f_send_sms_type'] == '0'){
            //根据包裹状态为发送中或发送失败时进行修改包裹状态  12-29
            $where = " `f_parcel_id` = {$data['f_parcel_id']} ";
            $parcelDetail = new \Home\Server\QueryParcelService();
            $list = $parcelDetail->queryParcelStatus($where);
            if ($list['f_parcel_status'] == '2' || $list['f_parcel_status'] == '5'){
                //根据发送状态更改包裹到站状态
                if ($status == '1'){
                    $parcelDetail->updateParcel(array('f_parcel_id'=>$data['f_parcel_id']),array('f_parcel_status'=>0));
                }else{
                    $parcelDetail->updateParcel(array('f_parcel_id'=>$data['f_parcel_id']),array('f_parcel_status'=>5));  //发送失败  12-23
                }
            }
        }
    }

    /**
     * @desc 查询12小时发送次数
     * @author quyang
     * @update 2016-12-17
     */
    public function querySmsNum($method){
        $times = date('YmdHis',time());
        $endtime = date('YmdHis',strtotime('-12 hour'));
        $where['_string'] = "f_send_time>=$endtime AND f_send_time<=$times";
        $where['f_parcel_id'] = $method;
        $data = $this->where($where)->count('f_parcel_id');
        return $data;
    }
}